package com.infoland.afanmall.model.my.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.entity.UserInFoEntity;
import com.infoland.afanmall.base.fragment.BaseLazyFragment;
import com.infoland.afanmall.base.fragment.LoginDialogFragment;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.UserKit;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.model.address.activity.AddressAllActivity;
import com.infoland.afanmall.model.miaoSha.activity.MiaoShaGoodInfoActivity;
import com.infoland.afanmall.model.my.activity.KeFuActivity;
import com.infoland.afanmall.model.my.activity.MsgActivity;
import com.infoland.afanmall.model.my.activity.OrderStatusActivity;
import com.infoland.afanmall.model.my.activity.PersonMesgActivity;
import com.infoland.afanmall.model.my.activity.SettingActivity;
import com.infoland.afanmall.model.my.activity.ShouCActivity;
import com.infoland.afanmall.model.order.activity.MyOrderActivity;
import com.infoland.afanmall.model.pinTuan.activity.PinTuanActivity;
import com.infoland.afanmall.model.zhongChou.activity.ZhongChouActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;

/***
 * 个人中心Fragment
 */
public class MyCenterFragment extends BaseLazyFragment {

    @BindView(R.id.my_img_ll_sz)
    LinearLayout myImgLlSz;
    @BindView(R.id.my_all_layout)
    LinearLayout myLayout3;
    @BindView(R.id.my_dai_fu)
    LinearLayout myDaifu;
    @BindView(R.id.my_yuan_text_1)
    AppCompatTextView myYuanText1;
    @BindView(R.id.my_dai_fa)
    LinearLayout myDaifa;
    @BindView(R.id.my_yuan_text_2)
    AppCompatTextView myYuanText2;
    @BindView(R.id.my_dai_shou)
    LinearLayout myDaishou;
    @BindView(R.id.my_yuan_text_3)
    AppCompatTextView myYuanText3;
    @BindView(R.id.my_ping_jia)
    LinearLayout myDaiping;
    @BindView(R.id.my_yuan_text_4)
    AppCompatTextView myYuanText4;
    @BindView(R.id.img_1)
    AppCompatImageView img1;
    @BindView(R.id.my_tuikuan)
    LinearLayout myTuikuan;
    @BindView(R.id.my_yuan_text_5)
    AppCompatTextView myYuanText5;
    @BindView(R.id.my_img1)
    AppCompatImageView myImg1;
    @BindView(R.id.my_img2)
    AppCompatImageView myImg2;
    @BindView(R.id.my_img3)
    AppCompatImageView myImg3;
    @BindView(R.id.my_person_msg)
    LinearLayoutCompat myLayout4;
    @BindView(R.id.ke_fu)
    LinearLayoutCompat myLayout6;
    @BindView(R.id.first_block)
    LinearLayout firstBlock;
    @BindView(R.id.my_text1)
    AppCompatTextView myText1;
    @BindView(R.id.my_layout1)
    LinearLayoutCompat myLayout1;
    @BindView(R.id.my_layout2)
    LinearLayoutCompat myLayout2;
    @BindView(R.id.my_img)
    AppCompatImageView myImg;
    @BindView(R.id.my_user_name)
    AppCompatTextView myUserName;
    @BindView(R.id.my_img_sz)
    ImageView my_img_sz;

    Unbinder unbinder;
    UserInFoEntity userInFoEntity;
    Object now_img_head; //当前头像
    int activityCode = 56;
    @BindView(R.id.miao_sha_layout)
    LinearLayoutCompat miaoShaLayout;
    @BindView(R.id.pin_tuan_layout)
    LinearLayoutCompat pinTuanLayout;
    @BindView(R.id.zhong_chou_layout)
    LinearLayoutCompat zhongChouLayout;

    public int getLayout() {
        return R.layout.my_center_fragment;
    }

    @Override
    public void initData() {
        if (!UserKit.isLogin()) {
            myUserName.setText("登录/注册");
        }
        addLinstener();
    }

    private void addLinstener() {
        /***没登录时点击用户名位置*/
        myUserName.setOnClickListener(v -> {
            if ("登录/注册".equals(myUserName.getText().toString())) {
                LoginDialogFragment loginDialogFragment = new LoginDialogFragment();
                loginDialogFragment.setShowBottom(true);
                loginDialogFragment.myCenterFragment = this;
                loginDialogFragment.show(getFragmentManager());
            } else {
                ToolsKit.go(getActivity(), PersonMesgActivity.class);
            }
        });
        /***个人信息*/
        myImg.setOnClickListener(v -> {
            if (UserKit.isLogin()) {
                ToolsKit.go(getActivity(), PersonMesgActivity.class);
            } else {
                ToastUtils.showShort("登陆后使用");
            }
        });
        /***消息**/
        myLayout2.setOnClickListener(v -> {
//            if (UserKit.isLogin()) {
            ToolsKit.go(getActivity(), MsgActivity.class, null);
//            } else {
//                ToastUtils.showShort("登陆后使用");
//            }
        });
        /***收藏**/
        myLayout1.setOnClickListener(v -> {
//            if (UserKit.isLogin()) {
            ToolsKit.go(getActivity(), ShouCActivity.class, null);
//            } else {
//                ToastUtils.showShort("登陆后使用");
//            }
        });

        /***设置**/
        myImgLlSz.setOnClickListener(v -> {
            ToolsKit.go(getActivity(), SettingActivity.class, null);
        });


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(getLayout(), container, false);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    /***获取登录信息**/
    public void getkLogin() {
        new OkHttpKit().send(
                response200 -> {
                    userInFoEntity = JSONObject.parseObject(response200, UserInFoEntity.class);
                    updateUserInFo();
                },
                error401 -> {
                    userInFoEntity = null;
                    updateUserInFo();
                },
                error -> {
                    userInFoEntity = null;
                    updateUserInFo();
                }
        ).post(CONST.USER_INFO + UserKit.getAccessToken(), new JSONObject(), getActivity());
    }

    /*** 更新用户信息**/
    private void updateUserInFo() {
        if (userInFoEntity == null) {
            ToolsKit.setImgYuanToResource(R.mipmap.morentx, myImg);
            myUserName.setText("登录/注册");
        } else {
            UserKit.putIsLogin(true);
            UserKit.putUserInFoEntity(JSONObject.toJSONString(userInFoEntity));
//            ToolsKit.
            switch (userInFoEntity.getU_type()) {
                /**微信用户**/
                case 0:
                    now_img_head = userInFoEntity.getWechat_headimgurl();
                    LogKit.E("   ", "  " + "  ");
                    break;
                /**QQ用户**/
                case 1:
                    now_img_head = userInFoEntity.getQq_headimgurl();
                    LogKit.E("   ", "  " + "  ");
                    break;
                /**注册用户**/
                case 2:
                    now_img_head = userInFoEntity.getHeadimgurl();
                    LogKit.E("   ", "  " + "  ");
                    break;
                default:
                    LogKit.E("   ", "  " + "  ");
                    break;
            }

            if (now_img_head == null) {
                LogKit.E(" now_img_head111  ", now_img_head + "");
                ToolsKit.setImgYuanToResource(R.mipmap.morentx, myImg);
            } else {
                LogKit.E(" now_img_head222  ", now_img_head.toString());
                ToolsKit.setImgYuanToUrl(now_img_head.toString(), myImg);
            }
            myUserName.setText("" + userInFoEntity.getU_username());
        }

    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        getkLogin();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    /*** 接收返回的ID**/
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogKit.E("  requestCode ", "  " + requestCode);
        LogKit.E("  resultCode ", "  " + resultCode);
        LogKit.E(" data  ", "  " + data);
        if (resultCode == 56) {
        }
    }


    @OnClick({R.id.my_all_layout, R.id.my_dai_fu, R.id.my_dai_fa,
            R.id.my_dai_shou, R.id.my_ping_jia, R.id.my_tuikuan,
            R.id.miao_sha_layout, R.id.pin_tuan_layout, R.id.zhong_chou_layout,
            R.id.my_person_msg, R.id.my_address, R.id.ke_fu
    })
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            /*** 秒杀**/
            case R.id.miao_sha_layout:
                ToolsKit.go(MiaoShaGoodInfoActivity.class);
                break;
            /*** 拼团**/
            case R.id.pin_tuan_layout:
                ToolsKit.go(PinTuanActivity.class);
                break;
            /*** 众筹**/
            case R.id.zhong_chou_layout:
                ToolsKit.go(ZhongChouActivity.class);
                break;

            /*** 全部订单**/
            case R.id.my_all_layout:
                intent = new Intent(getActivity(), MyOrderActivity.class);
                startActivityForResult(intent, Activity.RESULT_FIRST_USER);
                break;
            /*** 代付款**/
            case R.id.my_dai_fu:
                intent = new Intent(getActivity(), MyOrderActivity.class);
                startActivityForResult(intent, Activity.RESULT_FIRST_USER);
                break;
            /*** 代发货**/
            case R.id.my_dai_fa:
                intent = new Intent(getActivity(), MyOrderActivity.class);
                startActivityForResult(intent, Activity.RESULT_FIRST_USER);
                break;
            /*** 待收货**/
            case R.id.my_dai_shou:
                intent = new Intent(getActivity(), MyOrderActivity.class);
                startActivityForResult(intent, Activity.RESULT_FIRST_USER);
                break;
            /*** 评价**/
            case R.id.my_ping_jia:
                intent = new Intent(getActivity(), MyOrderActivity.class);
                startActivityForResult(intent, Activity.RESULT_FIRST_USER);
                break;
            /*** 退款**/
            case R.id.my_tuikuan:
                ToolsKit.go(OrderStatusActivity.class);
                break;
            /*** 个人信息界面**/
            case R.id.my_person_msg:
                ToolsKit.go(PersonMesgActivity.class);
                break;
            /*** 地址管理**/
            case R.id.my_address:
                ToolsKit.go(AddressAllActivity.class);
                break;
            /*** 联系我们**/
            case R.id.ke_fu:
                ToolsKit.go(KeFuActivity.class);
                break;

        }
    }

}
